<template>
  <doc-alert title="用户体系" url="https://doc.iocoder.cn/user-center/" />
  <doc-alert title="三方登陆" url="https://doc.iocoder.cn/social-user/" />
  <doc-alert title="Excel 导入导出" url="https://doc.iocoder.cn/excel-import-and-export/" />

  <!-- 搜索 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户名称" prop="key">
        <el-input
          v-model="queryParams.criteria.key"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="角色" prop="roleId">
        <el-select v-model="queryParams.criteria.roleId" placeholder="请选择角色" clearable class="!w-240px">
          <el-option
            v-for="dict in roleOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" />重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['member:add']"
        >
          <Icon icon="ep:plus" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="登录名" align="center" key="loginName" prop="loginName" />
      <el-table-column
        label="用户名称"
        align="center"
        prop="username"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="用户昵称"
        align="center"
        prop="nickname"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="登录次数"
        align="center"
        key="signinCount"
        prop="signinCount"
        :show-overflow-tooltip="true"
      >
      <template #default="scope">
        <el-tag type="primary" effect="dark" @click="handleSignInLog(scope.row.uid)">{{ scope.row.signinCount }}</el-tag>
      </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column label="操作" align="center" width="160">
        <template #default="scope">
          <div class="flex items-center justify-center">
            <el-button
              type="primary"
              link
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['member:edit']"
            >
              <Icon icon="ep:edit" />修改
            </el-button>
            <el-button
              v-hasPermi="['member:pwd-reset']"
              link
              type="warning"
              @click="handleResetPassword(scope.row)"
            >
              重置密码
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageIndex"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 添加或修改用户对话框 -->
  <UserForm ref="formRef" @success="getList" />
  <!-- 重置密码 -->
  <ResetPasswordForm ref="resetPasswordFormRef" @success="getList" />
  <!-- 登录日志 -->
  <SignInLog ref="signInLogRef" />
</template>
<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as UserApi from '@/api/system/user'
import UserForm from './UserForm.vue'
import ResetPasswordForm from './ResetPasswordForm.vue'
import SignInLog from './SignInLog.vue'

defineOptions({ name: 'SystemMember' })

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive<QueryCriteria>({
  pageIndex: 1,
  pageSize: 10,
  criteria:{
    key: undefined,
    roleId: undefined,
  },
  order: {
    property: 'id',
    direction: 'DESC'
  }
})
const queryFormRef = ref() // 搜索的表单
const roleOptions = ref<any[]>([]) // 角色列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageIndex = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 重置密码 */
const resetPasswordFormRef = ref()
const handleResetPassword = async (row: UserApi.MemberVO) => {
  resetPasswordFormRef.value.open(row.uid, row.loginName)
}

/** 登录日志 */
const signInLogRef = ref()
const handleSignInLog = async (uid: string) => {
  signInLogRef.value.open(uid)
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
